<?php

namespace Biz\service\brand;

use Biz\cache\brand\Brand as cBrand;
use Biz\model\brand\Brand as mBrand;
use Biz\model\brandAccount\BrandAccountInfo as mBrandAccountInfo;
use Biz\pockets\constInterface\BrandLevel as BrandLevelInterface;

class BrandLevel implements BrandLevelInterface
{

    /**
     * 品品牌等级列表
     * @return array
     */
    public static function levelList()
    {
        $levels = self::BRAND_LEVELS;

        $list = [];

        foreach ($levels as $level => $levelName) {
            $list[$level]['id'] = $level;
            $list[$level]['name'] = $levelName;
            $list[$level]['value_range'] = self::BRAND_LV_GROWTH_VALUE_RANGE[$level];
        }

        return $list;
    }

    /**
     * 获取品牌等级信息
     * @param int $brandId
     * @return array
     */
    public static function getBrandLevelInfo($brandId)
    {
        $levelInfo = [
            'level_label' => '',
            'level' => 0,
            'growth_value' => 0,
            'growth_value_upgrade' => 0,
            'next_level' => '',
            'next_level_label' => '',
            'level_upgrade_progress' => 0,
            'is_top_level' => self::TOP_LEVEL_NO,
            'next_level_growth_value' => 0,
            'icon' => '',
            'web_level_icon' => '',
        ];

        $brand = $brandId ? mBrand::getBrandInfoById($brandId) : [];

        if ($brand && is_array($brand)) {
            $levelInfo['level'] = $brand['level'];
            $levelInfo['level_label'] = self::BRAND_LEVELS[$brand['level']];
            $levelInfo['icon'] = self::BRAND_LEVEL_ICONS[$brand['level']];
            $levelInfo['web_level_icon'] = self::BRAND_MLEVEL_ICONS[$brand['level']];
            $levelInfo['growth_value'] = $brand['growth_value'];
            $levelInfo['is_top_level'] = $brand['level'] == self::BRAND_LEVEL_TOP ? self::TOP_LEVEL_YES : self::TOP_LEVEL_NO;
            [$levelGrowthStart, $levelGrowthEnd] = self::BRAND_LV_GROWTH_VALUE_RANGE[$brand['level']];
            $nextLevel = $brand['level'] + 1;
            $levelInfo['next_level'] = strval($nextLevel);
            if ($levelInfo['is_top_level']) {
                $nextLevelGrowthStart = $levelGrowthEnd;
                $nextLevelGrowthEnd = PHP_INT_MAX;
                $levelInfo['next_level_label'] = '';
            } else {
                [$nextLevelGrowthStart, $nextLevelGrowthEnd] = self::BRAND_LV_GROWTH_VALUE_RANGE[$nextLevel];
                $levelInfo['next_level_label'] = self::BRAND_LEVELS[$nextLevel];
            }
            $levelInfo['growth_value_upgrade'] = $nextLevelGrowthStart - $brand['growth_value'];
            $levelInfo['level_upgrade_progress'] = floor(($brand['growth_value'] - $levelGrowthStart) / ($nextLevelGrowthStart - $levelGrowthStart) * 100.00);
            $levelInfo['next_level_growth_value'] = $nextLevelGrowthStart;
        }

        return $levelInfo;

    }


    /**
     * 获取品牌账户信息
     * @param $brandId
     * @return array
     * @throws \Exception
     */
    private static function brandInfo($brandId)
    {
        $brand = mBrand::getBrandInfoById($brandId);
        if (empty($brand)) {
            throw new \Exception('非正在使用的品牌');
        }

        $accountInfo = mBrandAccountInfo::getOneByWhere([
            'type' => mBrandAccountInfo::ACCOUNT_TYPE_BRAND,
            'brand_id' => $brandId,
            'account_status' => mBrandAccountInfo::ACCOUNT_STATUS_USED
        ], ['id', 'in_start_time', 'in_end_time']);
        if (empty($accountInfo)) {
            throw new \Exception('品牌账户异常');
        }

        $brandInfo = [
            'brand_id' => $brandId,
            'uid' => $brand['uid'],
            'account_id' => $accountInfo['id'],
            'in_start_time' => strtotime($accountInfo['in_start_time']),
            'in_end_time' => strtotime($accountInfo['in_end_time']),
        ];

        return $brandInfo;

    }

    /**
     * 等级升级弹窗设置
     * @param $brandId
     * @param $level
     * @return bool
     */
    public static function setLevelUpgradeAlert($brandId, $level)
    {
        $cackeKey = 'level_upgrade_alert_' . $brandId;
        $alertInfo = [
            'level' => $level,
            'last_level' => $level - 1,
            'has_alert' => 0,
        ];
        cBrand::setItem($cackeKey, $alertInfo);
        return true;
    }


    /**
     * 品牌等级升级
     * @param $brandId
     * @param $level
     * @param $lastLevel
     * @return bool
     */
    public static function afterLevelUpgrade($brandId, $level, $lastLevel)
    {
        #参数校验
        if (!isset(self::BRAND_LEVELS[$level]) || !isset(self::BRAND_LEVELS[$lastLevel])) {
            throw new \Exception('等级参数错误!');
        }

        $brandInfo = self::brandInfo($brandId);

        #品牌等级权益升级

        #品牌升级弹窗设置
        self::setLevelUpgradeAlert($brandId, $level);

        return true;
    }

    /**
     * 品牌等级降级
     * @param $brandId
     * @param $level
     * @param $lastLevel
     * @return bool
     */
    public static function afterLevelDegrade($brandId, $level, $lastLevel)
    {
        #参数校验
        if (!isset(self::BRAND_LEVELS[$level]) || !isset(self::BRAND_LEVELS[$lastLevel])) {
            throw new \Exception('等级参数错误!');
        }

        $brandInfo = self::brandInfo($brandId);
        return true;
    }


}